package com.handpay.zztong.hp.accountcenter.activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.handpay.framework.ClientEngine;
import com.handpay.framework.HttpEngine;
import com.handpay.framework.LuaTableUtil;
import com.handpay.framework.SecureManager;
import com.handpay.framework.utils.CommonUtils;
import com.handpay.zztong.hp.R;
import com.handpay.zztong.hp.accountcenter.model.MicroMerchQueryBean;
import com.handpay.zztong.hp.base.activity.ZZTongActivity;
import com.handpay.zztong.hp.bean.ActionBar;
import com.handpay.zztong.hp.bean.ResponseBean;
import com.handpay.zztong.hp.config.ZZTConstant;
import com.handpay.zztong.hp.log.HPLog;
import com.handpay.zztong.hp.protocol.BaseProtocolKey;
import com.handpay.zztong.hp.protocol.UCProtocolKey;
import com.handpay.zztong.hp.protocol.UploadProtocolConstant;
import com.handpay.zztong.hp.utils.AccountUtils;

import java.util.Hashtable;

import se.krka.kahlua.vm.LuaTable;

/**
 * 资质信息
 */
public class QualificationInfoActivity extends ZZTongActivity implements View.OnClickListener {

    private LinearLayout ll_Checking;
    private TextView tv_Checking;
    private ScrollView sv_Success;
    private TextView tv_SuccessDate;
    private TextView tb_user_name;
    private TextView tb_certno;
    private TextView tb_shop_name;
    private TextView tb_main_bank_name;
    private TextView tb_card_no;
    private LinearLayout ll_CheckingFailure;
    private TextView tv_CheckingFailure;


    private int status = -1;
    private String name = null;
    private String certNo = null;
    private String shopName = null;
    private String bankName = null;
    private String mBankId = null;
    private String mSubBankId = null;
    private String mProvinceId = null;
    private String mAreaId = null;
    private String mProvinceName = null;
    private String mAreaName = null;
    private String cardNum = null;
    private String bankOfDeposit = null;

    private String frontCCPicStatus = null;
    private String frontCCPic = null;
    private String backCCPicStatus = null;
    private String backCCPic = null;
    private String holdPicStatus = null;
    private String holdPic = null;
    private String companyPicStatus = null;
    private String companyPic = null;
    private String BLicensePicStatus = null;
    private String BLicensePic = null;
    private ImageView open_province_line, open_city_line, open_subbank_line;
    private TextView open_province, open_city, open_subBank;
    private View mProvinceTable, mAreaTable, mSubBankTable;
    private MicroMerchQueryBean merchQueryBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.setContentView(R.layout.activity_qualification_info);
        super.onCreate(savedInstanceState);
        ll_Checking = (LinearLayout) findViewById(R.id.ll_Checking);
        tv_Checking = (TextView) findViewById(R.id.tv_Checking);
        sv_Success = (ScrollView) findViewById(R.id.sv_Success);
        tv_SuccessDate = (TextView) findViewById(R.id.tv_SuccessDate);
        tb_user_name = (TextView) findViewById(R.id.tb_user_name);
        tb_certno = (TextView) findViewById(R.id.tb_certno);
        tb_shop_name = (TextView) findViewById(R.id.tb_shop_name);
        tb_main_bank_name = (TextView) findViewById(R.id.tb_main_bank_name);
        tb_card_no = (TextView) findViewById(R.id.tb_card_no);

        ll_CheckingFailure = (LinearLayout) findViewById(R.id.ll_CheckingFailure);
        tv_CheckingFailure = (TextView) findViewById(R.id.tv_CheckingFailure);
        open_province = (TextView) this.findViewById(R.id.tb_opening_province);
        open_city = (TextView) this.findViewById(R.id.tb_opening_city);
        open_subBank = (TextView) this.findViewById(R.id.tb_sub_bank_name);
        mProvinceTable = this.findViewById(R.id.opening_province);
        mAreaTable = this.findViewById(R.id.opening_city);
        mSubBankTable = this.findViewById(R.id.open_sub_bank);

        open_province_line = (ImageView) this.findViewById(R.id.opening_province_line);
        open_city_line = (ImageView) this.findViewById(R.id.opening_city_line);
        open_subbank_line = (ImageView) this.findViewById(R.id.opening_sub_bank_line);

        ll_CheckingFailure.setOnClickListener(this);
        tv_CheckingFailure.setOnClickListener(this);
        merchQueryBean = new MicroMerchQueryBean();
    }

    @Override
    public ActionBar getActivityActionBar() {
        return new ActionBar(this.getString(R.string.QualityInformation), true);
    }

    protected boolean isShownTabs() {
        return false;
    }

    @Override
    protected void onStart() {
        super.onStart();
        queryMicroMarchInfo();
    }

    /***
     * 查询资质信息
     */
    private void queryMicroMarchInfo() {
        if (AccountUtils.getAccountPhone(this) == null) return;
        Hashtable<String, String> params = new Hashtable<>();
        params.put(BaseProtocolKey.ACCOUNT, SecureManager.getInstance().des(AccountUtils.getAccountName(this), 1, null));
        params.put(BaseProtocolKey.CHANNEL, ClientEngine.CHANNEL);
        params.put(BaseProtocolKey.NEWBANKFLAG, newBankFlag);
        doJsonPost(this, DO_zztGetAuditInfo, MicroMerchQueryBean.class, params);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /***
     * 设置状态
     *
     * @param merchQueryBean 资质信息
     */
    private void SettingState(MicroMerchQueryBean merchQueryBean) {
        int status = CommonUtils.objectToInt(merchQueryBean.getStatus());
        if (status == -1) {
            return;
        }
        if (status == 2) {
            ll_Checking.setVisibility(View.VISIBLE);
            ll_CheckingFailure.setVisibility(View.GONE);
            sv_Success.setVisibility(View.GONE);
            ((TextView) findViewById(R.id.checking_tip)).setText(this.getString(R.string.QualityAudit));
            if (!TextUtils.isEmpty(merchQueryBean.getAuditInfo())) {

                tv_Checking.setText(merchQueryBean.getAuditInfo());
            }
        } else if (status == 0) {
            ll_Checking.setVisibility(View.GONE);
            ll_CheckingFailure.setVisibility(View.GONE);
            sv_Success.setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(merchQueryBean.getAuditInfo())) {
                tv_SuccessDate.setText(merchQueryBean.getAuditInfo());
            }
            if (!TextUtils.isEmpty(merchQueryBean.getName())) {
                tb_user_name.setText(merchQueryBean.getName());
            }
            String certNo = merchQueryBean.getCertNo();
            if (!TextUtils.isEmpty(certNo)) {
                tb_certno.setText(CommonUtils.formattingCertNo4_2(certNo));
            }
            tb_shop_name.setText(merchQueryBean.getShopName());
            tb_main_bank_name.setText(merchQueryBean.getBankName());
            if (!TextUtils.isEmpty(merchQueryBean.getProvinceId()) && !TextUtils.isEmpty(merchQueryBean.getProvinceName())) {
                mProvinceTable.setVisibility(View.VISIBLE);
                open_province_line.setVisibility(View.VISIBLE);
                open_province.setText(merchQueryBean.getProvinceName());
            }
            if (!TextUtils.isEmpty(merchQueryBean.getAreaId()) && !TextUtils.isEmpty(merchQueryBean.getAreaName())) {
                mAreaTable.setVisibility(View.VISIBLE);
                open_city_line.setVisibility(View.VISIBLE);
                open_city.setText(merchQueryBean.getAreaName());
            }
            if (!TextUtils.isEmpty(merchQueryBean.getSubBankCode()) && !TextUtils.isEmpty(merchQueryBean.getBankOfDeposit())) {
                mSubBankTable.setVisibility(View.VISIBLE);
                open_subbank_line.setVisibility(View.VISIBLE);
                open_subBank.setText(merchQueryBean.getBankOfDeposit());
            }
            if (!TextUtils.isEmpty(merchQueryBean.getCardNum())) {
                tb_card_no.setText(CommonUtils.formattingCard_4(merchQueryBean.getCardNum()));
            }
        } else if (status == 3) {
            ll_Checking.setVisibility(View.GONE);
            ll_CheckingFailure.setVisibility(View.VISIBLE);
            sv_Success.setVisibility(View.GONE);
            tv_CheckingFailure.setText(merchQueryBean.getFailDescpriction());
        }
    }

    /***
     * @param
     */
    private void SettingState(String name, String certNo, String shopName, String bankName, String cardNum, String bankOfDeposit, String mAuditDes) {
        if (status == -1) {
            return;
        }
        if (status == 2) {
            ll_Checking.setVisibility(View.VISIBLE);
            ll_CheckingFailure.setVisibility(View.GONE);
            sv_Success.setVisibility(View.GONE);
            ((TextView) findViewById(R.id.checking_tip)).setText(this.getString(R.string.QualityAudit));
            if (!TextUtils.isEmpty(mAuditDes)) {
                tv_Checking.setText(mAuditDes);
            }
        } else if (status == 0) {
            ll_Checking.setVisibility(View.GONE);
            ll_CheckingFailure.setVisibility(View.GONE);
            sv_Success.setVisibility(View.VISIBLE);
            tv_SuccessDate.setText(mAuditDes);
            if (name != null) {
                tb_user_name.setText(name);
            }
            if (certNo != null) {
                String start = certNo.substring(0, 3);
                String end = certNo.substring(certNo.length() - 4);
                tb_certno.setText(start + " ********* " + end);
            }
            if (shopName != null) tb_shop_name.setText(shopName);
            if (shopName != null) tb_shop_name.setText(shopName);
            if (bankName != null) tb_main_bank_name.setText(bankName);
            if (!TextUtils.isEmpty(mProvinceId) && !TextUtils.isEmpty(mProvinceName)) {
                mProvinceTable.setVisibility(View.VISIBLE);
                open_province_line.setVisibility(View.VISIBLE);
                open_province.setText(mProvinceName);
            }
            HPLog.i("QualificationInfomation", "mAreaId" + mAreaId);
            if (!TextUtils.isEmpty(mAreaId) && !TextUtils.isEmpty(mAreaName)) {
                mAreaTable.setVisibility(View.VISIBLE);
                open_city_line.setVisibility(View.VISIBLE);
                HPLog.i("QualificationInfomation", "mAreaName" + mAreaName);
                open_city.setText(mAreaName);
            }
            if (!TextUtils.isEmpty(mSubBankId) && !TextUtils.isEmpty(bankOfDeposit)) {
                mSubBankTable.setVisibility(View.VISIBLE);
                open_subbank_line.setVisibility(View.VISIBLE);
                open_subBank.setText(bankOfDeposit);
            }
            if (cardNum != null) {
                tb_card_no.setText(CommonUtils.formattingCard_4(cardNum));
            }
            //tv_Checking
        } else if (status == 3) {
            ll_Checking.setVisibility(View.GONE);
            ll_CheckingFailure.setVisibility(View.VISIBLE);
            sv_Success.setVisibility(View.GONE);
            tv_CheckingFailure.setText(mAuditDes);
        }
    }

    @Override
    public boolean netJsonResponse(String action, ResponseBean resp, boolean showAlert) {
        if (super.netJsonResponse(action, resp, showAlert)) {
            this.setResult(Activity.RESULT_CANCELED);
            return true;
        }
        if (action.equals(DO_zztGetAuditInfo)) {
            merchQueryBean = (MicroMerchQueryBean) resp;
            if (!TextUtils.isEmpty(merchQueryBean.getStatus())) {
                String bankOfDeposit = merchQueryBean.getBankOfDeposit();//支行名称
                String name = SecureManager.getInstance().des(merchQueryBean.getName(), 0, null);
                merchQueryBean.setName(name);
                String desCertNo = SecureManager.getInstance().des(merchQueryBean.getCertNo(), 0, null);
                merchQueryBean.setCertNo(desCertNo);
                String cardNum = merchQueryBean.getCardNum();
                cardNum = SecureManager.getInstance().des(cardNum, 0, null);
                merchQueryBean.setCardNum(cardNum);
                String bankName = merchQueryBean.getBankName();
                bankName = SecureManager.getInstance().des(bankName, 0, null);
                merchQueryBean.setBankName(bankName);
                String shopName = merchQueryBean.getShopName();
                shopName = SecureManager.getInstance().des(shopName, 0, null);
                merchQueryBean.setShopName(shopName);
                if (!TextUtils.isEmpty(bankOfDeposit)) {
                    bankOfDeposit = SecureManager.getInstance().des(bankOfDeposit, 0, null);
                }
                merchQueryBean.setBankOfDeposit(bankOfDeposit);
                String provinceName = merchQueryBean.getProvinceName();
                if (!TextUtils.isEmpty(provinceName)) {
                    provinceName = SecureManager.getInstance().des(provinceName, 0, null);
                }
                merchQueryBean.setProvinceName(provinceName);
                String areaName = merchQueryBean.getAreaName();
                if (!TextUtils.isEmpty(areaName)) {
                    areaName = SecureManager.getInstance().des(areaName, 0, null);
                }
                merchQueryBean.setAreaName(areaName);
                AccountUtils.setAccountFailDes(this, merchQueryBean.getAuditInfo());
                SettingState(merchQueryBean);
                AccountUtils.setAccountStatus(this, AccountUtils.convertStatus(merchQueryBean.getStatus()));
                AccountUtils.setShopName(this, merchQueryBean.getShopName());
            }
            this.setResult(Activity.RESULT_OK);
        }
        return false;
    }

    @Override
    public boolean netResponse(String action, Hashtable<String, Object> resp, boolean showAlert) {
        boolean result = super.netResponse(action, resp, true, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
              dialog.dismiss();
            }
        });
        if (result) {
            this.setResult(Activity.RESULT_CANCELED);
            this.closeProgressDialog();
            return true;
        }
        if (action.equals(DO_zztGetAuditInfo)) {
            byte data[] = (byte[]) resp.get(HttpEngine.Key_RespData);
            LuaTable luaTable = LuaTableUtil.stringToLuaTable(data, isDataCompile);
            if (luaTable.rawget("status") != null) {
                String tempStatus = (String) luaTable.rawget("status");
                status = Integer.parseInt(tempStatus);
                name = (String) luaTable.rawget("name");
                certNo = (String) luaTable.rawget("certNo");
                shopName = (String) luaTable.rawget("shopName");
                bankName = (String) luaTable.rawget("bankName");
                cardNum = (String) luaTable.rawget("cardNum");
                bankOfDeposit = (String) luaTable.rawget("bankOfDeposit");    //支行名称
                name = SecureManager.getInstance().des(name, 0, null);
                cardNum = SecureManager.getInstance().des(cardNum, 0, null);
                bankName = SecureManager.getInstance().des(bankName, 0, null);
                certNo = SecureManager.getInstance().des(certNo, 0, null);
                shopName = SecureManager.getInstance().des(shopName, 0, null);
                String mAuditDes = (String) luaTable.rawget("auditInfo");
                frontCCPicStatus = (String) luaTable.rawget("frontCCPicStatus");
                frontCCPic = (String) luaTable.rawget("frontCCPic");
                backCCPicStatus = (String) luaTable.rawget("backCCPicStatus");
                backCCPic = (String) luaTable.rawget("backCCPic");
                holdPic = (String) luaTable.rawget("holdPic");
                holdPicStatus = (String) luaTable.rawget("holdPicStatus");
                companyPicStatus = (String) luaTable.rawget("companyPicStatus");
                companyPic = (String) luaTable.rawget("companyPic");
                BLicensePicStatus = (String) luaTable.rawget("BLicensePicStatus");
                BLicensePic = (String) luaTable.rawget("BLicensePic");
                mBankId = (String) luaTable.rawget(UCProtocolKey.KEY_BANKCODE);
                mSubBankId = (String) luaTable.rawget("subBankCode");
                if (!TextUtils.isEmpty(bankOfDeposit)) {
                    bankOfDeposit = SecureManager.getInstance().des(bankOfDeposit, 0, null);
                }
                this.mProvinceId = (String) luaTable.rawget(UCProtocolKey.KEY_PROVIVCEID);
                this.mProvinceName = (String) luaTable.rawget(UCProtocolKey.KEY_PROVINCENAME);
                if (!TextUtils.isEmpty(mProvinceName)) {
                    mProvinceName = SecureManager.getInstance().des(mProvinceName, 0, null);
                }
                this.mAreaId = (String) luaTable.rawget(UCProtocolKey.KEY_AREAID);
                this.mAreaName = (String) luaTable.rawget(UCProtocolKey.KEY_AREANAME);
                if (!TextUtils.isEmpty(mAreaName)) {
                    mAreaName = SecureManager.getInstance().des(mAreaName, 0, null);
                }
                SettingState(name, certNo, shopName, bankName, cardNum, bankOfDeposit, mAuditDes);
                AccountUtils.setAccountFailDes(this, mAuditDes);
                AccountUtils.setAccountStatus(this, AccountUtils.convertStatus(tempStatus));
                AccountUtils.setShopName(this, shopName);
            }
            this.setResult(Activity.RESULT_OK);
            this.closeProgressDialog();
        }
        return false;
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        int i = v.getId();
        if (i == R.id.ll_CheckingFailure) {// 先检查是否连接刷卡器，再去填写资质材料信息
            //重新提交的情况，不需要检查刷卡器是否插入，和刷卡器的状态
            HPLog.i(HPLog.YL_TAG, "onClick:1");
            goToVPos();
        }
    }

    @Override
    public void goToVPos() {
        if (checkVPOS(this, true)) {
            HPLog.d(HPLog.SSX_TAG, "merchQueryBean:" + merchQueryBean.toString());
            startEditMerchInfo(merchQueryBean);
//            sendComposeBseInfo();
        }
    }

    /***
     * 资质提交审核失败，去重新提交
     *
     * @param merchQueryBean 查询到的资质信息
     */
    public void startEditMerchInfo(MicroMerchQueryBean merchQueryBean) {
        if (CommonUtils.objectToInt(merchQueryBean.getStatus()) == 3) {
            Intent intent = new Intent(ZZTConstant.RECOMPOSE_ACTION);
            intent.setPackage(getPackageName());
            intent.putExtra(UCProtocolKey.KEY_NAME, merchQueryBean.getName());
            intent.putExtra(UCProtocolKey.KEY_CERTNO, merchQueryBean.getCertNo());
            intent.putExtra(UCProtocolKey.KEY_SHOPNAME, merchQueryBean.getShopName());
            intent.putExtra(UCProtocolKey.KEY_BANKNAME, merchQueryBean.getBankName());
            intent.putExtra(UCProtocolKey.KEY_BANKCODE, merchQueryBean.getBankCode());
            if (!TextUtils.isEmpty(merchQueryBean.getSubBankCode()) && !TextUtils.isEmpty(merchQueryBean.getBankOfDeposit())) {
                intent.putExtra(UCProtocolKey.KEY_SUBBANKCODE, merchQueryBean.getSubBankCode());
                intent.putExtra(UCProtocolKey.KEY_SUBBANKNAME, merchQueryBean.getBankOfDeposit());
            }
            if (!TextUtils.isEmpty(merchQueryBean.getProvinceId()) && !TextUtils.isEmpty(merchQueryBean.getProvinceName())) {
                intent.putExtra(UCProtocolKey.KEY_PROVIVCEID, merchQueryBean.getProvinceId());
                intent.putExtra(UCProtocolKey.KEY_PROVINCENAME, merchQueryBean.getProvinceName());
            }
            if (!TextUtils.isEmpty(merchQueryBean.getAreaId()) && !TextUtils.isEmpty(merchQueryBean.getAreaName())) {
                intent.putExtra(UCProtocolKey.KEY_AREAID, merchQueryBean.getAreaId());
                intent.putExtra(UCProtocolKey.KEY_AREANAME, merchQueryBean.getAreaName());
            }
            intent.putExtra(UCProtocolKey.KEY_CARDNUM, merchQueryBean.getCardNum());
            intent.putExtra(UploadProtocolConstant.KEY_FRONTCCPICSTATUS, merchQueryBean.getFrontCCPicStatus());
            intent.putExtra(UploadProtocolConstant.KEY_FRONTCCPIC, merchQueryBean.getFrontCCPic());
            intent.putExtra(UploadProtocolConstant.KEY_BACKCCPICSTATUS, merchQueryBean.getBackCCPicStatus());
            intent.putExtra(UploadProtocolConstant.KEY_BACKCCPIC, merchQueryBean.getBackCCPic());
            intent.putExtra(UploadProtocolConstant.KEY_HOLDPIC, merchQueryBean.getHoldPic());
            intent.putExtra(UploadProtocolConstant.KEY_HOLDPICSTATUS, merchQueryBean.getHoldPicStatus());
            intent.putExtra(UploadProtocolConstant.KEY_COMPANYPICSTATUS, merchQueryBean.getCompanyPicStatus());
            intent.putExtra(UploadProtocolConstant.KEY_COMPANYPIC, merchQueryBean.getCompanyPic());
            intent.putExtra(UploadProtocolConstant.KEY_BLICENSEPICSTATUS, merchQueryBean.getBLicensePicStatus());
            intent.putExtra(UploadProtocolConstant.KEY_BLICENSEPIC, merchQueryBean.getBLicensePic());
            this.startActivity(intent);
        } else {
            showAlertDialog(this, getString(R.string.prompt, merchQueryBean.getStatus()));
        }
    }

    /***
     * 失败后去填写资质信息
     */
    public void sendComposeBseInfo() {
        if (status != 3) return;
        Intent intent = new Intent(ZZTConstant.RECOMPOSE_ACTION);
        intent.setPackage(getPackageName());
        intent.putExtra(UCProtocolKey.KEY_NAME, name);
        intent.putExtra(UCProtocolKey.KEY_CERTNO, certNo);
        intent.putExtra(UCProtocolKey.KEY_SHOPNAME, shopName);
        intent.putExtra(UCProtocolKey.KEY_BANKNAME, bankName);
        intent.putExtra(UCProtocolKey.KEY_BANKCODE, mBankId);
        if (!TextUtils.isEmpty(mSubBankId) && !TextUtils.isEmpty(bankOfDeposit)) {
            intent.putExtra(UCProtocolKey.KEY_SUBBANKCODE, mSubBankId);
            intent.putExtra(UCProtocolKey.KEY_SUBBANKNAME, bankOfDeposit);
        }
        if (!TextUtils.isEmpty(mProvinceId) && !TextUtils.isEmpty(mProvinceName)) {
            intent.putExtra(UCProtocolKey.KEY_PROVIVCEID, mProvinceId);
            intent.putExtra(UCProtocolKey.KEY_PROVINCENAME, mProvinceName);
        }
        if (!TextUtils.isEmpty(mAreaId) && !TextUtils.isEmpty(mAreaName)) {
            intent.putExtra(UCProtocolKey.KEY_AREAID, mAreaId);
            intent.putExtra(UCProtocolKey.KEY_AREANAME, mAreaName);
        }
        intent.putExtra(UCProtocolKey.KEY_CARDNUM, cardNum);
        intent.putExtra(UploadProtocolConstant.KEY_FRONTCCPICSTATUS, frontCCPicStatus);
        intent.putExtra(UploadProtocolConstant.KEY_FRONTCCPIC, frontCCPic);
        intent.putExtra(UploadProtocolConstant.KEY_BACKCCPICSTATUS, backCCPicStatus);
        intent.putExtra(UploadProtocolConstant.KEY_BACKCCPIC, backCCPic);
        intent.putExtra(UploadProtocolConstant.KEY_HOLDPIC, holdPic);
        intent.putExtra(UploadProtocolConstant.KEY_HOLDPICSTATUS, holdPicStatus);
        intent.putExtra(UploadProtocolConstant.KEY_COMPANYPICSTATUS, companyPicStatus);
        intent.putExtra(UploadProtocolConstant.KEY_COMPANYPIC, companyPic);
        intent.putExtra(UploadProtocolConstant.KEY_BLICENSEPICSTATUS, BLicensePicStatus);
        intent.putExtra(UploadProtocolConstant.KEY_BLICENSEPIC, BLicensePic);
        this.startActivity(intent);
    }

}
